package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class RegisterVisitor
 */
@WebServlet("/RegisterVisServlet")
public class RegisterVisServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterVisServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");  
	    
	    
	    String name=request.getParameter("name");  
	    String lastname=request.getParameter("lastname");
	    String email=request.getParameter("email");
	    String address=request.getParameter("address");
	    int phone= Integer.parseInt(request.getParameter("phone"));  
	    String dni=request.getParameter("dni");  
	    String username=request.getParameter("username");
	    String password=request.getParameter("password");  
	    		   
	    Visitor u=new Visitor();
	   
	    u.setName(name);
	    u.setLastname(lastname);
	    u.setMail(email);
	    u.setAddress(address);
	    u.setPhone(phone);
	    u.setDni(dni);
	    u.setUsername(username);
	    u.setPassword(password);
	    
	    VisitorService vs=new VisitorService();
	
	    
	    
	    try{
	    	boolean tt =vs.registerVisitor(u);
		    if (tt==true){	
		    	String msg= "You have been registered on our website. Now LogIn to access.";
				request.setAttribute("msg",msg);  	    
		    RequestDispatcher rd=request.getRequestDispatcher("mainpage.jsp");  
		    rd.forward(request,response);
		    
		    }else{
		    	String msg= "Sorry, register wrong";
				request.setAttribute("msg",msg);  
		    	RequestDispatcher rd2=request.getRequestDispatcher("mainpage.jsp");  
		         rd2.forward(request,response);  
		         }
	    }catch(Exception ex){
			String msg= ex.getMessage();
			request.setAttribute("msg",msg); 
			RequestDispatcher dispatcher = request.getRequestDispatcher("mainpage.jsp");
            dispatcher.forward(request, response);
		
		}
		 
		          
		 
		
	}
	    

}
